﻿package net.wangit.view.element.component;

import net.wangit.Path;
import net.wangit.Row;
import net.wangit.context.ActionContext;
import net.wangit.framework.exception.ConfigurationException;
import net.wangit.support.AbstractElementWriter;
import net.wangit.util.Checker;
import net.wangit.util.FileUtil;
import net.wangit.util.XMLUtil;
import net.wangit.view.element.ElementComponent;
import net.wangit.view.element.util.ElementUtil;
import net.wangit.view.util.Html;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.dom4j.Document;


public class FlashElementWriter extends AbstractElementWriter {

	private static final Log log = LogFactory
			.getLog(net.wangit.view.element.component.FlashElementWriter.class);
	private static final String DEFAULT_FLASH_CLASSID = "clsid:D27CDB6E-AE6D-11cf-96B8-444553540000";
	private static final String DEFAULT_FLASH_CODEBASE = "http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,29,0";
	private static final String DEFAULT_FLASH_TYPE = "application/x-shockwave-flash";
	private static final String DEFAULT_FLASH_PLUGINSPAGE = "http://www.macromedia.com/go/getflashplayer";
	private static String flashClassId;
	private static String flashCodeBase;
	private static String flashType;
	private static String flashPluginsPage;

	public FlashElementWriter() {
		if (Checker.isEmpty(flashClassId))
			load();
	}

	private void load() {
		String path = (new StringBuilder(String.valueOf(Path
				.getDomainPath("dev")))).append("flash.xml").toString();
		try {
			if (!FileUtil.exists(path)) {
				log.error((new StringBuilder("flash file not found:")).append(
						path).toString());
				throw new ConfigurationException("global file not found!");
			}
			Document document = XMLUtil.parseByPath(path);
			org.dom4j.Element flash = document.getRootElement();
			flashClassId = XMLUtil.getTextString(flash, "classid",
					FlashElementWriter.DEFAULT_FLASH_CLASSID);
			flashCodeBase = XMLUtil.getTextString(flash, "codebase",
					FlashElementWriter.DEFAULT_FLASH_CODEBASE);
			flashType = XMLUtil.getTextString(flash, "type",
					FlashElementWriter.DEFAULT_FLASH_TYPE);
			flashPluginsPage = XMLUtil.getTextString(flash, "pluginspage",
					FlashElementWriter.DEFAULT_FLASH_PLUGINSPAGE);
		} catch (Exception e) {
			log.error("load flash settings failed!");
			throw new ConfigurationException(path, e);
		}
		log.info("load flash settings successfully.");
	}

	protected void writeText(ActionContext ac, StringBuffer html,
			ElementComponent ec, Row row) {
		writeControl(ac, html, ec, row);
	}

	protected void writeTextWithValue(ActionContext ac, StringBuffer html,
			ElementComponent ec, Row row) {
		writeControl(ac, html, ec, row);
	}

	protected void writeControl(ActionContext ac, StringBuffer html,
			ElementComponent ec, Row row) {
		writeControlWithDisplay(ac, html, ec, row, false);
	}

	protected void writeReadOnlyControl(ActionContext ac, StringBuffer html,
			ElementComponent ec, Row row) {
		writeControl(ac, html, ec, row);
	}

	protected void writeDisabledControl(ActionContext actioncontext,
			StringBuffer stringbuffer, ElementComponent elementcomponent,
			Row row1) {
	}

	protected void writeHidden(ActionContext ac, StringBuffer html,
			ElementComponent ec, Row row) {
		writeControlWithDisplay(ac, html, ec, row, true);
	}

	private void writeControlWithDisplay(ActionContext ac, StringBuffer html,
			ElementComponent ec, Row row, boolean hidden) {
		int width = ec.getWidth();
		int height = ec.getHeight();
		String link = ElementUtil.resolveLink(ac, ec, row);
		html.append("<object classid=\"").append(flashClassId).append(
				"\" codebase=\"").append(flashCodeBase).append("\"");
		if (hidden)
			html.append(" id=\"").append(ElementUtil.getAlias(ec)).append(
					"\" style=\"display:none\"");
		Html.setAttribute(html, ElementUtil.resolveCustom(ac, ec, row));
		Html.setCss(html, ec.getCss());
		Html.setWidth(html, width);
		Html.setHeight(html, height);
		html.append(">");
		html.append("<param name=\"movie\" value=\"").append(link)
				.append("\">");
		html.append("<param name=\"quality\" value=\"high\">");
		html.append("<embed src=\"").append(link).append(
				"\" quality=\"high\" type=\"").append(flashType).append("\"")
				.append(" pluginspage=\"").append(flashPluginsPage)
				.append("\"");
		Html.setWidth(html, width);
		Html.setHeight(html, height);
		html.append("></embed>");
		html.append("</object>");
	}

}
